﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Experimental.GlobalIllumination;
using UnityEngine.Serialization;
using UnityEngine.UI;

public  class MActionStateType : ActionStateType
{
    public const int win = 6;
    public const int fail = 7;
    public const int battle = 8;
    public const int fever = 9;
}

/// <summary>
/// player controller
/// </summary>
public partial class Player : RoleEntity
{
    public int score;
    public MBattleController mBattleController => battleController as MBattleController;

    public bool win => curAction != null && curAction.stateId == MActionStateType.win;
    public bool fail => dead || curAction != null && curAction.stateId == MActionStateType.fail;

    /// <summary>
    /// transform animation tools,used to move to target pos
    /// </summary>
    ZTransformAnim zTransformAnim;
    public ZTransformAnim TransformAnim
    {
        get
        {
            if (zTransformAnim == null) zTransformAnim = this.gameObject.AddComponent<ZTransformAnim>();
            return zTransformAnim;
        }
    }

    [SerializeField]
    private UserInfo m_Data;
    public UserInfo data => m_Data;

    //public Slider incomeProgress;
    public TMPro.TextMeshPro incomeText;
    public TMPro.TextMeshPro scoreText;

    protected override void Awake()
    {
        base.Awake();

#if UNITY_EDITOR
        this.moveHorFactor /= 2;
#endif
    }

    public void Init(BattlePlayerData playerData, MBattleDataConfig dataConfig)
    {
        m_Hp = maxHp;
        m_Data = playerData.fiveDimensions;
        this.m_ActionFSMSystem = InitPlayerActionFSM(this.battleController, this);
        UpdateWeight(playerData.fiveDimensions.weight,false);
        UpdateHeight(playerData.fiveDimensions.height,false);
        income = playerData.fiveDimensions.income;
        m_EduScore = 0;
    }

    public void ShowSkin(int skinId)
    {
        m_RoleId = skinId;
        //TODO SKin
    }
    
    protected override void FixedUpdate()
    {
        if (curAction == null || dead || achieved) return;

        bool moveHorRigid = true;//curAction.stateId == MActionStateType.run;
        
        if (Rigidbody)
        {
            Vector3 moveVec = this.curMoveSpeed * moveForwardValue * transform.forward;//m_CtrlMoveVec.normalized;
            if (moveHorRigid)
            {
                moveVec +=  m_MoveHor * transform.TransformDirection(Vector3.right);
                //moveVec.x = this.m_MoveHor;
            }
            moveVec.y = this.Rigidbody.velocity.y;
            this.Rigidbody.velocity = moveVec;
        }
    }

    public override void ReceiveDamage(int damage)
    {
        base.ReceiveDamage(damage);
        
        GameDebug.LogError($"{name} ReceiveDamage:{damage}");
    }

    protected override void Update()
    {
        base.Update();

        /*if (incomeProgress)
        {
            incomeProgress.value = 
        }*/
    }
    

    public PlayerActionFSM InitPlayerActionFSM(BattleController battleController, Player role)
    {
        PlayerActionFSM fsm = new PlayerActionFSM(battleController, role);

        ActionStateIdle idle = new ActionStateIdle(fsm);
        ActionStateRun run = new ActionStateRun(fsm);
        ActionStatePlayerAttack playerAttack = new ActionStatePlayerAttack(fsm);
        ActionStatePlayerDie die = new ActionStatePlayerDie(fsm);
        
        ActionStateWin win = new ActionStateWin(fsm);

        Idle2RunTransition idle2Run = new Idle2RunTransition(idle, run);
        Run2IdleTransition run2Idle = new Run2IdleTransition(run, idle);

        SignTransition any2Attack = new SignTransition(MActionStateType.attack, playerAttack);
        ProgressActionTransition attackEnd = new ProgressActionTransition(playerAttack,run);
        
        Any2DieTransition any2Die = new Any2DieTransition(die);
        SignTransition any2Win = new SignTransition(MActionStateType.win, win);
        
        fsm.anyState.AddTransition(any2Die);
        fsm.anyState.AddTransition(any2Win);

        idle.AddTransition(idle2Run);
        run.AddTransition(run2Idle);
        run.AddTransition(any2Attack);
        
        playerAttack.AddTransition(attackEnd);

        fsm.SetDefaultState(idle);

        return fsm;
    }

}